package com.example.demo.controller;

import com.example.demo.bean.RegionStates;
import com.example.demo.index.PageVO;
import com.example.demo.index.TestService;
import com.example.demo.service.DataProvider;
import com.example.demo.service.RiskAreaService;
import com.example.demo.service.UserInfoService;
import com.example.demo.service.UserService;
import com.example.demo.util.TransferUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class PageController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private DataProvider dataProvider;
    @Autowired
    private RiskAreaService riskAreaService;
    @Autowired
    TestService testService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/toMain")
    public String getCoronaVirusData(Model model) throws Exception {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        String type = userService.login(username).getType();
        if(type.equals("管理员")){
            return "admin";
        }else {
            PageVO pageVO = PageVO.builder()
                    .now(new Date())
                    .confirmed(testService.getConfirmed())
                    .recovered(testService.getRecovered())
                    .deaths(testService.getDeaths())
                    .nowConfirm(testService.getNowConfirm())
                    .build();
            model.addAttribute("data", pageVO);
            return "main";
        }
    }


    @RequestMapping("/failure")
    public String failure(Model model){
        model.addAttribute("loginFail","T");
        return "login";
    }

    @RequestMapping("/zhuye")
    public String zhuye(Model model) throws Exception {
        PageVO pageVO = PageVO.builder()
                .now(new Date())
                .confirmed(testService.getConfirmed())
                .recovered(testService.getRecovered())
                .deaths(testService.getDeaths())
                .nowConfirm(testService.getNowConfirm())
                .build();
        model.addAttribute("data", pageVO);
        return "main";
    }

    @RequestMapping("/register")
    public String register(){
        return "user-register";
    }

    @RequestMapping("/global1")
    public String foreign()
    {
        return "foreign";
    }

    @RequestMapping("/record")
    public String record(){
        String username = "";
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal().getClass().toString().equals("class java.lang.String")){
            username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }else {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            username = principal.getUsername();
        }
        if(userInfoService.getUserInfoByUsername(username).getIdtype() == null){
            return "not-report";
        }
        return "report-record";
    }

    @RequestMapping("/china")
    public String showByTotal(Model model) throws IOException {
        List<RegionStates> list = dataProvider.getRegionStates("China");
        TransferUtil.transferToChinese(list);
        model.addAttribute("rsList",list);
        System.out.println(list);
        return "china";
    }

    @RequestMapping("/regional")
    public String jjk1(Model model) throws IOException {
        List<RegionStates> list = dataProvider.getNum();
        model.addAttribute("rsList",list);
        System.out.println(list);
        return "global";
    }

    @RequestMapping("/userMange")
    public String userMange(){
        return "user-Mange";
    }

    @RequestMapping("/details/{id}")
    public String details(@PathVariable("id")Integer id, HttpSession session){
        session.setAttribute("id", id);
        return "user-details";
    }

    @RequestMapping("/notreport")
    public String notreport(){
        return "user-notreport";
    }

    @RequestMapping("/numberMange")
    public String numberMange(){
        return "usernumber-Mange";
    }

    @RequestMapping("/lockaccount/{id}")
    public String lockaccount(@PathVariable("id")Integer id){
        userService.updateUserStatus1(id);
        return "redirect:/numberMange";
    }

    @RequestMapping("/unlockaccount/{id}")
    public String unlockaccount(@PathVariable("id")Integer id){
        userService.updateUserStatus2(id);
        return "redirect:/numberMange";
    }

    @RequestMapping("/lockaccount1/{id}")
    public String lockaccount1(@PathVariable("id")Integer id){
        userService.updateUserStatus1(id);
        return "redirect:/adminMange";
    }

    @RequestMapping("/unlockaccount1/{id}")
    public String unlockaccount1(@PathVariable("id")Integer id){
        userService.updateUserStatus2(id);
        return "redirect:/adminMange";
    }

    @RequestMapping("/riskareaMange")
    public String riskareaMange(){
        return "riskarea-Mange";
    }

    @RequestMapping("/admin1")
    public String admin1(){
        return "admin";
    }

    @RequestMapping("/adminMange")
    public String adminMange(){
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        if(username.equals("201841412200")){
            return "adminMange";
        }else {
            return "not-superAdmin";
        }
    }

}
